package com.jy.rhin.application.assembler;

import cn.hutool.core.collection.CollUtil;
import cn.org.chiss.rhin._2015.base.*;
import cn.org.chiss.rhin._2015.base.object.MainID;
import cn.org.chiss.rhin._2015.base.object.ObjectStatus;
import cn.org.chiss.rhin._2015.base.object.ObjectStatusPrimitive;
import cn.org.chiss.rhin._2015.base.terminology.*;
import com.jy.api.misc.dto.ZhiYuYSDMDto;
import com.jy.api.misc.dto.ZhiYuYSMXDto;
import com.jy.framework.base.utils.DateUtil;

import java.lang.String;
import java.util.Date;

public class ConceptMapAssembler {
    private ConceptMapAssembler() {
    }

    public static ConceptMap toConceptMap(ZhiYuYSMXDto dto) {
        ConceptMap conceptMap = new ConceptMap();

        conceptMap.setId((MainID) new MainID().setValue(dto.getYingSheBM()));
        conceptMap.setStatusCode(new ObjectStatus().setValue(ObjectStatusPrimitive.ACTIVE));
        conceptMap.setName(setValue(dto.getYingSheMC()));
        /** 发布者 */
        conceptMap.setPublisher(setValue(dto.getYuanZhiYuCS()));
        conceptMap.setDescription(setValue(""));
        Period period = new Period();
        period.setStart(new DateTime().setValue(DateUtil.getYYMMDD(new Date()).replaceAll("-","")));
        period.setEnd(new DateTime().setValue(DateUtil.getYYMMDD(new Date()).replaceAll("-","")));
        conceptMap.setEffectiveTime(period);

        //设置源值域
        Reference srouce = new Reference();
        srouce.setDisplay(setValue(dto.getYuanZhiYuMC()));
        Identifier sr_dentifier = new Identifier();
        sr_dentifier.setValue(new ID().setValue(dto.getYuanZhiYuID()));
        srouce.setReference(sr_dentifier);
        conceptMap.setSource(srouce);


        //设置目标值域
        Reference target = new Reference();
        target.setDisplay(setValue(dto.getMuBiaoZYMC()));
        Identifier tar_dentifier = new Identifier();
        tar_dentifier.setValue(new ID().setValue(dto.getMuBiaoZYID()));
        target.setReference(tar_dentifier);
        conceptMap.setTarget(target);

        if (CollUtil.isEmpty(dto.getZhiYuYSBMXTList())) {
            return conceptMap;
        }
        for (ZhiYuYSDMDto ysdm : dto.getZhiYuYSBMXTList()) {
            //映射的编码..循环设置element
            ConceptMapElement mapElement = new ConceptMapElement();
            //源编码值
            mapElement.setCode(new Code().setValue(ysdm.getYuanZhi()));
            //源编码系统
            mapElement.setCodeSystem(new OID().setValue(ysdm.getYuanBMXTBSF()));
            //目标编码
            ConceptMapTarget mapTarget = new ConceptMapTarget();
            //目标编码值
            mapTarget.setCode(setValue(ysdm.getMuBiaoZhi()));
            //目标编码系统
            mapTarget.setCodeSystem(new OID().setValue(ysdm.getMuBiaoBMXTBSF()));
            //编码映射关联,默认EQUIVALENT（相等，等价的）
            EquivalenceType equivalenceType = new EquivalenceType();
            equivalenceType.setValue(EquivalencePrimitive.EQUIVALENT);
            mapTarget.setEquivalence(equivalenceType);
            mapElement.setTarget(mapTarget);
            conceptMap.getElement().add(mapElement);
        }
        return conceptMap;
    }

    private static cn.org.chiss.rhin._2015.base.String setValue(String value) {
        return new cn.org.chiss.rhin._2015.base.String().setValue(value);
    }
}
